<template>
  <div class="login-container" :class="{ 'dark-mode': isDarkMode }">
    <el-card class="login-card">
      <div class="login-header">
        <h2>{{ $t('login.title') }}</h2>
        <div class="theme-toggle">
          <el-button circle @click="toggleTheme">
            <el-icon>
              <component :is="isDarkMode ? 'Sunny' : 'Moon'" />
            </el-icon>
          </el-button>
          <el-button circle @click="toggleLocale">
            <span>{{ locale === 'zh-CN' ? 'EN' : '中' }}</span>
          </el-button>
        </div>
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-position="top"
      >
        <el-form-item :label="$t('common.username')" prop="username">
          <el-input
            v-model="loginForm.username"
            :placeholder="$t('common.username')"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item :label="$t('common.password')" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            :placeholder="$t('common.password')"
            prefix-icon="Lock"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.remember">{{ $t('login.rememberMe') }}</el-checkbox>
          <a class="forgot-password">{{ $t('login.forgotPassword') }}</a>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            {{ $t('login.loginButton') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/modules/user'
import { useConfigStore } from '@/store/modules/config'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'
import { User, Lock, Sunny, Moon } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()
const configStore = useConfigStore()
const { t, locale } = useI18n()

const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const loginRules = {
  username: [
    { required: true, message: () => t('common.username') + '不能为空', trigger: 'blur' }
  ],
  password: [
    { required: true, message: () => t('common.password') + '不能为空', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)
const loading = ref(false)

// 主题设置
const isDarkMode = computed(() => configStore.theme === 'dark')
const toggleTheme = () => configStore.toggleTheme()

// 切换语言
const toggleLocale = () => {
  const newLocale = locale.value === 'zh-CN' ? 'en-US' : 'zh-CN'
  locale.value = newLocale
  configStore.setLocale(newLocale)
}

// 登录处理
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const success = await userStore.login(loginForm.username, loginForm.password)
        if (success) {
          ElMessage.success(t('login.loginSuccess'))
          router.push('/dashboard')
        }
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.dark-mode {
  background-color: #1f1f1f;
}

.login-card {
  width: 400px;
  max-width: 90%;
}

.login-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.theme-toggle {
  display: flex;
  gap: 10px;
}

.login-button {
  width: 100%;
}

.forgot-password {
  float: right;
  color: #409EFF;
  cursor: pointer;
}
</style>